import 'package:equatable/equatable.dart';
import 'package:backend.purchases/domain/entities/yookassa_api/payments/yookassa_api_payment_cancellation_details.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/canceled_payment/yookassa_api_payment_cancellation_party_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/canceled_payment/yookassa_api_payment_cancellation_reason_dto.dart';

const kYookassaApiPaymentCancellationDetailsPartyKey = 'party';
const kYookassaApiPaymentCancellationDetailsReasonKey = 'reason';

class YookassaApiPaymentCancellationDetailsDto extends Equatable {
  const YookassaApiPaymentCancellationDetailsDto._internal({
    required this.partyDto,
    required this.reasonDto,
  });

  final YookassaApiPaymentCancellationPartyDto partyDto;
  final YookassaApiPaymentCancellationReasonDto reasonDto;

  @override
  List<Object?> get props {
    return [
      partyDto,
      reasonDto,
    ];
  }

  factory YookassaApiPaymentCancellationDetailsDto.fromJson(Map<String, dynamic> json) {
    final partyString = json[kYookassaApiPaymentCancellationDetailsPartyKey] as String;
    final partyDto = YookassaApiPaymentCancellationPartyDto.fromString(partyString);

    final reasonString = json[kYookassaApiPaymentCancellationDetailsReasonKey] as String;
    final reasonDto = YookassaApiPaymentCancellationReasonDto.fromString(reasonString);

    return YookassaApiPaymentCancellationDetailsDto._internal(
      partyDto: partyDto,
      reasonDto: reasonDto,
    );
  }

  YookassaApiPaymentCancellationDetails toEntity() {
    final party = partyDto.toEntity();
    final reason = reasonDto.toYookassaApiPaymentCancellationReason();

    return YookassaApiPaymentCancellationDetails(
      party: party,
      reason: reason,
    );
  }
}
